﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Assignment3.Skyboxes
{
    class Skybox
    {
        private Model _skybox;
        private TextureCube _skyboxTexture;
        private Effect _skyboxEffect;
        private float _boxSize = 500f;
        
        public Skybox(Model skybox, TextureCube textureCube, Effect skyboxEffect)
        {
            _skybox = skybox;
            _skyboxTexture = textureCube;
            _skyboxEffect = skyboxEffect;
        }

        public void Draw(Matrix view, Matrix projection, Vector3 cameraPosition)
        {
            // Go through each pass in the effect, but we know there is only one...
            foreach (EffectPass pass in _skyboxEffect.CurrentTechnique.Passes)
            {
                // Draw all of the components of the mesh, but we know the cube really
                // only has one mesh
                foreach (ModelMesh mesh in _skybox.Meshes)
                {
                    // Assign the appropriate values to each of the parameters
                    foreach (ModelMeshPart part in mesh.MeshParts)
                    {
                        part.Effect = _skyboxEffect;
                        part.Effect.Parameters["World"].SetValue(Matrix.CreateScale(_boxSize) * Matrix.CreateTranslation(cameraPosition));
                        part.Effect.Parameters["View"].SetValue(view);
                        part.Effect.Parameters["Projection"].SetValue(projection);
                        part.Effect.Parameters["SkyBoxTexture"].SetValue(_skyboxTexture);
                        part.Effect.Parameters["CameraPosition"].SetValue(cameraPosition);
                    }

                    // Draw the mesh with the skybox effect
                    mesh.Draw();
                }
            }
        }
    }
}
